package com.zcbl.toolkit.map.baidu.direction;

import com.zcbl.toolkit.map.baidu.OutputEnum;
import com.zcbl.toolkit.map.baidu.coord.CoordEnum;
import com.zcbl.toolkit.map.baidu.http.BaiduTextJavaScriptFastJsonHttpMessageConverter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Objects;

/**
 * @author he peng
 * @create 2018/4/24 9:53
 * @see
 */
public class RoutePlanner {

    private static final Logger LOG = LoggerFactory.getLogger(RoutePlanner.class);

    private String DEFAULT_REQUEST_URL = "http://api.map.baidu.com/direction/v2/";
    private String requestUrl = DEFAULT_REQUEST_URL;
    private String requestKey = System.getProperty("baidu.map.api.key");
    private RestTemplate DEFAULT_REST_TEMPLATE = new RestTemplate();
    private RestTemplate restTemplate = DEFAULT_REST_TEMPLATE;

    public RoutePlanner() {
        List<HttpMessageConverter<?>> messageConverters = this.restTemplate.getMessageConverters();
        messageConverters.add(new BaiduTextJavaScriptFastJsonHttpMessageConverter());
        this.restTemplate.setMessageConverters(messageConverters);
    }

    /*public RoutePlanner(String requestUrl, String requestKey, RestTemplate restTemplate) {
        this.requestUrl = requestUrl;
        this.requestKey = requestKey;
        this.restTemplate = restTemplate;
    }*/

    public RidingRouteJsonResponse riding(StartPointDestinationPair startPointDestinationPair , CoordEnum input , CoordEnum output , RidingEnum re , OutputEnum oe) {
        Assert.notNull(startPointDestinationPair , "startPointDestinationPair == null");
        Assert.notNull(input , "input == null");
        Assert.notNull(output , "output == null");
        Assert.notNull(re , "re == null");
        String requestUrl = parseRidingApiRequestUrl(startPointDestinationPair , input , output , re , oe);
        return this.restTemplate.getForObject(requestUrl, RidingRouteJsonResponse.class);
    }

    private String parseRidingApiRequestUrl(StartPointDestinationPair startPointDestinationPair , CoordEnum input, CoordEnum output , RidingEnum re , OutputEnum oe) {
        String ridingApiRequestUrl = new StringBuilder(this.requestUrl)
                    .append("/riding?origin=")
                    .append(startPointDestinationPair.getStartLat())
                    .append(",")
                    .append(startPointDestinationPair.getStartLng())
                    .append("&destination=")
                    .append(startPointDestinationPair.getDestinationLat())
                    .append(",")
                    .append(startPointDestinationPair.getDestinationLng())
                    .append("&coord_type=")
                    .append(StringUtils.lowerCase(input.name()))
                    .append("&ret_coordtype=")
                    .append(StringUtils.lowerCase(output.name()))
                    .append("&riding_type=")
                    .append(re.getValue())
                .toString();
        if (Objects.nonNull(oe)) {
            ridingApiRequestUrl = spliceOutput(ridingApiRequestUrl , oe);
        }
        return spliceRequestKey(ridingApiRequestUrl);
    }

    private String spliceOutput(String ridingApiRequestUrl, OutputEnum oe) {
        return ridingApiRequestUrl + "&output=" + StringUtils.lowerCase(oe.name());
    }

    private String spliceRequestKey(String requestUrl) {
        return requestUrl + "&ak=" + this.requestKey;
    }

}
